package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Stack;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btn_one;
    private Button btn_two;
    private Button btn_three;
    private Button btn_four;
    private Button btn_five;
    private Button btn_six;
    private Button btn_seven;
    private Button btn_eignt;
    private Button btn_nine;
    private Button btn_zero;
    private Button btn_num;
    private Button btn_chu;
    private Button btn_cheng;
    private Button btn_jian;
    private Button btn_add;
    private Button btn_dian;
    private Button btn_dengyu;
    private EditText ed_cal;
    private boolean opflag;
    private Stack<Integer> stack = new Stack<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_one = findViewById(R.id.btn_1);
        btn_two = findViewById(R.id.btn_2);
        btn_three = findViewById(R.id.btn_3);
        btn_four = findViewById(R.id.btn_4);
        btn_five = findViewById(R.id.btn_5);
        btn_six = findViewById(R.id.btn_6);
        btn_seven = findViewById(R.id.btn_7);
        btn_eignt = findViewById(R.id.btn_8);
        btn_nine = findViewById(R.id.btn_9);
        btn_zero = findViewById(R.id.btn_0);
        btn_num = findViewById(R.id.btn_10);
        btn_chu = findViewById(R.id.btn_11);
        btn_cheng = findViewById(R.id.btn_12);
        btn_jian = findViewById(R.id.btn_13);
        btn_add = findViewById(R.id.btn_add);
        btn_dian = findViewById(R.id.btn_15);
        btn_dengyu = findViewById(R.id.btn_16);
        ed_cal = findViewById(R.id.edittext_calculate);



        btn_one.setOnClickListener(this);
        btn_two.setOnClickListener(this);
        btn_three.setOnClickListener(this);
        btn_four.setOnClickListener(this);
        btn_five.setOnClickListener(this);
        btn_six.setOnClickListener(this);
        btn_seven.setOnClickListener(this);
        btn_eignt.setOnClickListener(this);
        btn_nine.setOnClickListener(this);
        btn_zero.setOnClickListener(this);
        btn_num.setOnClickListener(this);
        btn_chu.setOnClickListener(this);
        btn_cheng.setOnClickListener(this);
        btn_jian.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        btn_dian.setOnClickListener(this);
        btn_dengyu.setOnClickListener(this);


    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_1:
                if(opflag){
                    opflag = false;
                    ed_cal.setText("1");
                }else {
                    ed_cal.append("1");
                }
                /*if(ed_cal.getText() == null){
                    ed_cal.setText("1");
                }
                else{
                    ed_cal.setText(ed_cal.getText().toString()+"1");
                    ed_cal.append("1");
                }*/
                //Toast.makeText(MainActivity.this, "点击了数字1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_2:
                if(opflag){
                opflag = false;
                ed_cal.setText("2");
            }else {
                ed_cal.append("2");
            }
                Toast.makeText(MainActivity.this, "点击了数字2", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_3:
                if(opflag){
                    opflag = false;
                    ed_cal.setText("3");
                }else {
                    ed_cal.append("3");
                }
                Toast.makeText(MainActivity.this, "点击了数字3", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_4:
                if(opflag){
                    opflag = false;
                    ed_cal.setText("4");
                }else {
                    ed_cal.append("4");
                }
                Toast.makeText(MainActivity.this, "点击了数字4", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_5:
                if(opflag){
                    opflag = false;
                    ed_cal.setText("5");
                }else {
                    ed_cal.append("5");
                }
                Toast.makeText(MainActivity.this, "点击了数字5", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_6:
                if(opflag){
                    opflag = false;
                    ed_cal.setText("6");
                }else {
                    ed_cal.append("6");
                }
                Toast.makeText(MainActivity.this, "点击了数字6", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_7:
                if(opflag){
                    opflag = false;
                    ed_cal.setText("7");
                }else {
                    ed_cal.append("7");
                }
                Toast.makeText(MainActivity.this, "点击了数字7", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_8:
                if(opflag){
                    opflag = false;
                    ed_cal.setText("8");
                }else {
                    ed_cal.append("8");
                }
                Toast.makeText(MainActivity.this, "点击了数字8", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_9:
                if(opflag){
                    opflag = false;
                    ed_cal.setText("9");
                }else {
                    ed_cal.append("9");
                }
                Toast.makeText(MainActivity.this, "点击了数字9", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_0:
                if(opflag){
                    opflag = false;
                    ed_cal.setText("0");
                }else {
                    ed_cal.append("0");
                }
                Toast.makeText(MainActivity.this, "点击了数字0", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_10:
                Toast.makeText(MainActivity.this, "点击了num", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_11:
                opflag = true;
                if(stack.empty()){
                    int a=Integer.valueOf(ed_cal.getText().toString());
                    stack.push(a);
                }else{
                    int rs =  stack.pop() / Integer.valueOf(ed_cal.getText().toString());
                    stack.push(rs);
                    ed_cal.setText(String.valueOf(rs));
                }
                Toast.makeText(MainActivity.this, "点击了/", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_12:
                opflag = true;
                if(stack.empty()){
                    int a=Integer.valueOf(ed_cal.getText().toString());
                    stack.push(a);
                }else{
                    int rs =  stack.pop() * Integer.valueOf(ed_cal.getText().toString());
                    stack.push(rs);
                    ed_cal.setText(String.valueOf(rs));
                }
                Toast.makeText(MainActivity.this, "点击了*", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_13:
                opflag = true;
                if(stack.empty()){
                    int a=Integer.valueOf(ed_cal.getText().toString());
                    stack.push(a);
                }else{
                    int rs =  stack.pop() - Integer.valueOf(ed_cal.getText().toString());
                    stack.push(rs);
                    ed_cal.setText(String.valueOf(rs));
                }
                Toast.makeText(MainActivity.this, "点击了-", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_add:
                opflag = true;
                if(stack.empty()){
                    int a=Integer.valueOf(ed_cal.getText().toString());
                    stack.push(a);
                }else{
                   int rs =  stack.pop() + Integer.valueOf(ed_cal.getText().toString());
                   stack.push(rs);
                   ed_cal.setText(String.valueOf(rs));
                }

                Toast.makeText(MainActivity.this, "点击了+", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_15:
                Toast.makeText(MainActivity.this, "点击了.", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_16:

                Toast.makeText(MainActivity.this, "点击了=", Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(MainActivity.this, "点击了其他按钮", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}